package com.example.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.article.service.ArticleConfigService;
import com.example.common.constants.NewsMessageConstants;
import com.example.model.article.pojo.ArticleConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class ArticleIsDownListener {
    @Autowired
    private ArticleConfigService articleConfigService;

    @KafkaListener(topics = NewsMessageConstants.NEWS_UP_OR_DOWN_TOPIC)
    public void onMessage(String message) {
        Map map = JSON.parseObject(message, Map.class);
        Object enable = map.get("enable");
        boolean isDown = true;
        if (enable.equals(1)) {
            isDown = false;
        }
        // 修改文章
        QueryWrapper< ArticleConfig > wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", map.get("articleId"));
        ArticleConfig articleConfig = new ArticleConfig();
        articleConfig.setIsDown(isDown);
        articleConfigService.update(articleConfig, wrapper);
    }
}
